/*
 * Copyright (c) 2011, grossmann
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the name of the jo-widgets.org nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL jo-widgets.org BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

package joodin.impl.widgets.internal;

import java.util.HashSet;
import java.util.Set;

import joodin.impl.image.VaadinImageRegistry;

import org.jowidgets.common.color.IColorConstant;
import org.jowidgets.common.image.IImageConstant;
import org.jowidgets.common.types.Markup;
import org.jowidgets.common.types.Position;
import org.jowidgets.common.widgets.controler.IPopupDetectionListener;
import org.jowidgets.spi.impl.controler.TreeNodeObservable;
import org.jowidgets.spi.widgets.IPopupMenuSpi;
import org.jowidgets.spi.widgets.ITreeNodeSpi;
import org.jowidgets.util.Assert;

import com.vaadin.data.Item;
import com.vaadin.terminal.Resource;

public class TreeNodeImpl extends TreeNodeObservable implements ITreeNodeSpi {

	private final Set<IPopupDetectionListener> popupDetectionListeners;

	private final TreeImpl parentTree;
	private final Item item;
	private final Integer id;

	public TreeNodeImpl(final TreeImpl parentTree, final Integer parentItemId, final Integer index) {
		super();
		Assert.paramNotNull(parentTree, "parentTree");

		this.popupDetectionListeners = new HashSet<IPopupDetectionListener>();

		this.parentTree = parentTree;
		
		parentTree.getUiReference().getItemIds();
		
		this.id  = (Integer) parentTree.getUiReference().addItem();
		this.item = parentTree.getUiReference().getItem(id);
		
		parentTree.getUiReference().setChildrenAllowed(id, false);

		if (index != null) {
			if (parentItemId != null) {
				parentTree.getUiReference().setParent(id, parentItemId);
			}
		}
		else {
			if (parentItemId != null) {
				parentTree.getUiReference().setParent(id, parentItemId);
			}
		}
	}

	@Override
	public Item getUiReference() {
		return item;
	}

	@Override
	public void setEnabled(final boolean enabled) {
		if (!enabled) {
			throw new UnsupportedOperationException("Tree items could not be disabled");
		}
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public void setText(final String text) {
		if (text != null) {
			parentTree.getUiReference().setItemCaption(id, text);
		}
		else {
			parentTree.getUiReference().setItemCaption(id, "");
		}
	}

	@Override
	public void setToolTipText(final String toolTipText) {
		//TODO MG
	}

	@Override
	public void setIcon(final IImageConstant icon) {
		if(icon == null){
			return;
		}
		
		Resource res = VaadinImageRegistry.getInstance().getImage(icon);
		
		parentTree.getUiReference().setItemIcon(id,res );
	}

	@Override
	public void setMarkup(final Markup markup) {
//		final Font newFont = FontProvider.deriveFont(item.getFont(), markup);
//		item.setFont(newFont);
	}

	@Override
	public void setForegroundColor(final IColorConstant colorValue) {
//		getUiReference().setForeground(ColorCache.getInstance().getColor(colorValue));
	}

	@Override
	public void setBackgroundColor(final IColorConstant colorValue) {
//		getUiReference().setBackground(ColorCache.getInstance().getColor(colorValue));
	}

	@Override
	public void setExpanded(final boolean expanded) {
		if(expanded){
			parentTree.getUiReference().expandItem(id);
		}
		else{
			parentTree.getUiReference().collapseItem(id);
		}
	}

	@Override
	public boolean isExpanded() {
		return parentTree.getUiReference().isExpanded(id);
	}

	@Override
	public void setSelected(final boolean selected) {
		if(selected){
			parentTree.getUiReference().select(id);
		}
		else{
			parentTree.getUiReference().unselect(id);
		}
	}

	@Override
	public boolean isSelected() {
		return parentTree.getUiReference().isSelected(id);
	}

	@Override
	public void addPopupDetectionListener(final IPopupDetectionListener listener) {
		popupDetectionListeners.add(listener);
	}

	@Override
	public void removePopupDetectionListener(final IPopupDetectionListener listener) {
		popupDetectionListeners.remove(listener);
	}

	protected void firePopupDetected(final Position position) {
		for (final IPopupDetectionListener listener : popupDetectionListeners) {
			listener.popupDetected(position);
		}
	}

	@Override
	public ITreeNodeSpi addNode(final Integer index) {
		parentTree.getUiReference().setChildrenAllowed(id, true);
		final TreeNodeImpl result = new TreeNodeImpl(parentTree, id, index);
		parentTree.registerItem(result.getUiReference(), result);
		return result;
	}

	@Override
	public void removeNode(final int index) {
		parentTree.unRegisterItem(getUiReference());
		if(parentTree.getUiReference().getChildren(parentTree.getUiReference().getParent(id)).size() < 1){
			parentTree.getUiReference().setChildrenAllowed(parentTree.getUiReference().getParent(id), false);
		}
		parentTree.getUiReference().removeItem(id);
	}

	@Override
	public IPopupMenuSpi createPopupMenu() {
		return new PopupMenuImpl(parentTree.getUiReference());
	}

}
